<template>
  <div class="search-bar">
    <el-input
      v-model="searchKeyword"
      placeholder="搜索商品..."
      class="search-input"
      clearable
      @keyup.enter="handleSearch"
      @clear="handleClear"
    >
      <template #prefix>
        <el-icon class="search-icon"><Search /></el-icon>
      </template>
      <template #suffix>
        <el-button 
          type="primary" 
          :icon="Search" 
          @click="handleSearch"
          class="search-btn"
          size="small"
        >
          搜索
        </el-button>
      </template>
    </el-input>

    <!-- 高级搜索 -->
    <el-popover
      placement="bottom-start"
      :width="400"
      trigger="click"
      v-model:visible="showAdvancedSearch"
    >
      <template #reference>
        <el-button 
          :icon="Filter" 
          circle 
          class="filter-btn"
          :class="{ active: hasActiveFilters }"
        />
      </template>
      
      <div class="advanced-search">
        <h4>高级搜索</h4>
        
        <el-form :model="filters" label-width="80px" size="small">
          <el-form-item label="分类">
            <el-select v-model="filters.category" placeholder="选择分类" clearable>
              <el-option
                v-for="category in goodsStore.categories"
                :key="category.categoryId"
                :label="category.name"
                :value="category.name"
              >
                <span>{{ category.icon }} {{ category.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          
          <el-form-item label="价格区间">
            <div class="price-range">
              <el-input-number 
                v-model="filters.minPrice" 
                placeholder="最低价"
                :min="0"
                :precision="2"
                controls-position="right"
              />
              <span class="price-separator">-</span>
              <el-input-number 
                v-model="filters.maxPrice" 
                placeholder="最高价"
                :min="0"
                :precision="2"
                controls-position="right"
              />
            </div>
          </el-form-item>
          
          <el-form-item label="成色">
            <el-select v-model="filters.condition" placeholder="选择成色" clearable>
              <el-option label="全新" value="全新" />
              <el-option label="九成新" value="九成新" />
              <el-option label="八成新" value="八成新" />
              <el-option label="七成新" value="七成新" />
              <el-option label="六成新及以下" value="六成新及以下" />
            </el-select>
          </el-form-item>
          
          <el-form-item>
            <div class="filter-actions">
              <el-button @click="resetFilters">重置</el-button>
              <el-button type="primary" @click="applyFilters">应用筛选</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-popover>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useGoodsStore } from '@/stores/goodsStore'
import { useRouter } from 'vue-router'
import { Search, Filter } from '@element-plus/icons-vue'
import { debounce } from '@/utils/format'

const goodsStore = useGoodsStore()
const router = useRouter()

// 搜索关键词
const searchKeyword = ref('')
const showAdvancedSearch = ref(false)

// 高级搜索筛选条件
const filters = ref({
  category: '',
  minPrice: null,
  maxPrice: null,
  condition: ''
})

// 是否有激活的筛选条件
const hasActiveFilters = computed(() => {
  return filters.value.category || 
         filters.value.minPrice || 
         filters.value.maxPrice || 
         filters.value.condition
})

// 防抖搜索
const debouncedSearch = debounce(() => {
  if (searchKeyword.value.trim()) {
    goodsStore.searchGoods(searchKeyword.value.trim())
    // 确保在首页显示搜索结果
    if (router.currentRoute.value.path !== '/') {
      router.push('/')
    }
  }
}, 500)

// 监听搜索关键词变化
watch(searchKeyword, () => {
  if (searchKeyword.value.trim()) {
    debouncedSearch()
  }
})

// 处理搜索
const handleSearch = () => {
  if (searchKeyword.value.trim()) {
    goodsStore.searchGoods(searchKeyword.value.trim())
    if (router.currentRoute.value.path !== '/') {
      router.push('/')
    }
  }
}

// 清除搜索
const handleClear = () => {
  goodsStore.resetSearch()
  goodsStore.getGoodsList()
}

// 应用筛选
const applyFilters = () => {
  const filterParams = {}
  
  if (filters.value.category) filterParams.category = filters.value.category
  if (filters.value.minPrice) filterParams.minPrice = filters.value.minPrice
  if (filters.value.maxPrice) filterParams.maxPrice = filters.value.maxPrice
  if (filters.value.condition) filterParams.condition = filters.value.condition
  
  goodsStore.filterGoods(filterParams)
  showAdvancedSearch.value = false
  
  if (router.currentRoute.value.path !== '/') {
    router.push('/')
  }
}

// 重置筛选
const resetFilters = () => {
  filters.value = {
    category: '',
    minPrice: null,
    maxPrice: null,
    condition: ''
  }
  goodsStore.resetSearch()
  goodsStore.getGoodsList()
  showAdvancedSearch.value = false
}
</script>

<style scoped>
.search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.search-input {
  flex: 1;
}

.search-input :deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 25px;
  padding: 0 15px;
  transition: all 0.3s ease;
}

.search-input :deep(.el-input__wrapper):hover {
  background-color: white;
  border-color: rgba(255, 255, 255, 0.5);
}

.search-input :deep(.el-input__wrapper.is-focus) {
  background-color: white;
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.search-input :deep(.el-input__inner) {
  color: #333;
  font-size: 14px;
}

.search-input :deep(.el-input__inner::placeholder) {
  color: #999;
}

.search-icon {
  color: #999;
}

.search-btn {
  background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
  border: none;
  border-radius: 15px;
  padding: 6px 12px;
  font-size: 12px;
}

.filter-btn {
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  transition: all 0.3s ease;
}

.filter-btn:hover,
.filter-btn.active {
  background-color: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: scale(1.05);
}

.advanced-search {
  padding: 10px 0;
}

.advanced-search h4 {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.price-range {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.price-range .el-input-number {
  flex: 1;
}

.price-separator {
  color: #999;
  font-weight: 500;
}

.filter-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 10px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-bar {
    width: 100%;
  }
  
  .search-input :deep(.el-input__wrapper) {
    border-radius: 20px;
  }
  
  .advanced-search {
    width: 300px;
  }
  
  .price-range {
    flex-direction: column;
    gap: 5px;
  }
  
  .price-separator {
    display: none;
  }
}
</style>